package name.jiaosha.air.company.controller;

import java.util.ArrayList;
import java.util.List;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.company.domain.Bug;
import name.jiaosha.air.company.service.ICompanyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.PageInfo;
import com.ali8ba.common.PinyinToolkit;

/**
 * 故障字典控制器
 * 
 * @author xu
 * 
 */
@Controller
@RequestMapping("/admin/com")
public class BugController {

	@Autowired
	private ICompanyService companyService;

	@RequestMapping("/bugList")
	public ModelAndView getMyBugList() {

		PageInfo<Bug> pageInfo = null;
		if (!AppConstants.DEBUG) {
			List<Bug> list = new ArrayList<Bug>();
			for (int i = 1; i <= 20; i++) {
				Bug bug = new Bug();
				bug.setId(i);
				bug.setName("内机损坏" + i);
				bug.setPinyin(PinyinToolkit.cn2FirstSpell(bug.getName()));
				list.add(bug);
			}

		} else {

			pageInfo = companyService.getBugListByComId(AppConstants.comId, 1,
					AppConstants.DEFAULT_SIZE);

		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", pageInfo.getItemList());
		model.addAttribute("totalProperty", pageInfo.getTotalProperty());
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/createBug")
	public ModelAndView save(@RequestParam String name) {

		if (!AppConstants.DEBUG) {

		} else {

			Bug bug = new Bug();
			bug.setComId(AppConstants.comId);
			bug.setName(name.trim());

			companyService.createBug(bug);
		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/updateBug")
	public ModelAndView update(@RequestParam int id, @RequestParam String name) {

		if (!AppConstants.DEBUG) {

		} else {
			Bug bug = companyService.getBugById(id);
			if (bug != null) {
				bug.setName(name.trim());

				companyService.updateBug(bug);
			}

		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}
}
